package com.fesnakj.todoer.ui;

import com.fesnakj.todoer.data.TaskPool;
import com.fesnakj.todoer.model.TaskImpl;
import com.vaadin.annotations.Theme;
import com.vaadin.data.Property;
import com.vaadin.data.util.AbstractBeanContainer;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.server.ThemeResource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.*;

import java.util.List;

@Theme("mytheme")
public class TasksUI extends UI {

    List<TaskImpl> myTasks = TaskPool.getTasks();
    private static int counter = 100;
    BeanContainer<Integer, TaskImpl> container;
    private Table tasksTable;

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);


        Layout toolbar = new HorizontalLayout();
      
        
        tasksTable = buildTaskTable("", myTasks);

        Button addButton = new Button();
        addButton.setSizeUndefined();
        addButton.setIcon(new ThemeResource("ic_plus_small.png"));
        addButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent clickEvent) {
                container.addBean(new TaskImpl(++counter, "TaskImpl" + counter));
            }
        });
        toolbar.addComponent(addButton);


        Button removeButton = new Button();
        removeButton.setIcon(new ThemeResource("ic_delete_small.png"));
        toolbar.addComponent(removeButton);


        layout.addComponent(toolbar);
        layout.addComponent(tasksTable);


        setContent(layout);
    }

    private Table buildTaskTable(String name, List<TaskImpl> tasks) {

        String[] columnHeaders = {"Title", "State", ""};
        String[] visilbleColumns = {"title", "state", "remove"};

        container = new BeanContainer<Integer, TaskImpl>(TaskImpl.class);
        container.setBeanIdResolver(new AbstractBeanContainer.BeanIdResolver<Integer, TaskImpl>() {
            @Override
            public Integer getIdForBean(TaskImpl task) {
                return task.getId();
            }
        });
        container.addAll(tasks);
        Table table = new Table(name, container);
        table.setColumnWidth("title", 600);

        table.setColumnReorderingAllowed(true);
        table.setSelectable(true);
        table.setMultiSelect(true);
        table.setColumnHeaderMode(Table.ColumnHeaderMode.HIDDEN);


        table.addGeneratedColumn("remove", new Table.ColumnGenerator() {
            @Override
            public Component generateCell(final Table source, final Object itemId, final Object columnId) {
                Button remove = new Button();
                remove.setIcon(new ThemeResource("ic_delete_small.png"));

                return remove;
            }
        });

        table.setVisibleColumns(visilbleColumns);
        table.setColumnHeaders(columnHeaders);
        table.addValueChangeListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(Property.ValueChangeEvent valueChangeEvent) {

            }
        });

        return table;
    }

    private boolean isAnySelected() {
        List<Integer> ids = container.getItemIds();
        for (Integer id : ids) {
            if (tasksTable.isSelected(id))
                return true;
        }
        return false;
    }


}
